package com.eaoa.uc.account.action;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.eaoa.base.EABaseAction;
import com.eaoa.query.pub.PubAccountQuery;
import com.eaoa.query.pub.PubRoleQuery;
import com.eaoa.vo.pub.PubAccount;
import com.eaoa.vo.pub.PubRole;
import com.stony.core.util.CoreUtils;

public class AccountRoleSettingPreAction extends EABaseAction{

	protected ActionForward doWork(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		Long employeeno = CoreUtils.parseLong(request.getParameter("accountno"));
		
		Set myroles = new HashSet();
		PubAccountQuery employeeQuery = new PubAccountQuery();
		employeeQuery.setEmployeeno(employeeno);
		employeeQuery.setLazy(true);
		employeeQuery.setLazyInitPubRoles(true);
		employeeQuery.execute();
		List employees =  employeeQuery.getResults();
		if(employees.size()==1){
			PubAccount employee = (PubAccount)employees.get(0);
			myroles = employee.getPubRoles();
		}
		
		PubRoleQuery roleQuery = new PubRoleQuery();
		Iterator myroleIt = myroles.iterator();
		while(myroleIt.hasNext()){
			PubRole role  = (PubRole)myroleIt.next();
			roleQuery.setNOEQRoleno(role.getRoleno());
		}	
		roleQuery.setSort();
		roleQuery.execute();
		
		List otherroles = roleQuery.getResults();
		
		request.setAttribute("accountno", employeeno);
		request.setAttribute("otherroles", otherroles);
		request.setAttribute("myroles", myroles);
		
		
		return mapping.findForward("success");
	}
}